package com.servlet.request; /**
 * @author wzm
 * @create 2023-06-21 9:29
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

@WebServlet("/rs2")
public class RequestServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /* String name = request.getParameter("name");
        System.out.println("name = " + name);*/

        //通用的代码调用一个就行
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求参数的编码
        request.setCharacterEncoding("UTF-8");

        //通用的获取参数的方法
        //1.获取请求所有参数作为一个Map集合
        Map<String, String[]> map = request.getParameterMap();
        //循环去除map中的所有key
        for (String key : map.keySet()){
            System.out.println("key = " + key);//username password

            String[] strings = map.get(key);

            //取的值是数组  我们需要遍历取出里面的值
            for (String value:strings){
                System.out.println("值为：" + value);
            }
            System.out.println("--------------------");
        }


        //2.根据参数的名称获取值 数组 针对多个值的属性
        String[] hobbies = request.getParameterValues("hobby");
        for (String s : hobbies){
            System.out.println("s = " + s);
        }

        //3.根据参数的名称获取值 指定input的name属性 获取单个值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String hobby = request.getParameter("hobby");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("hobby = " + hobby);

        //处理完了 我想去另外的地方？  另一个Servlet  另一资源


    }
}
